# !/usr/bin/env python
# coding:utf-8

import psutil
import datetime
import time

#查看cpu个数
def getCpuCount():
    print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))
    return psutil.cpu_count(logical=False)

# cpu的使用率
def getCpuPercent():
    cpu = (str(psutil.cpu_percent(1)))
    print("cup使用率: %s" % cpu)
    return cpu

# 查看内存信息,剩余内存.free  总共.total
# round()函数方法为返回浮点数x的四舍五入值。
def getMem():
    free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
    total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
    memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)
    print("物理内存： %s G" % total)
    print("剩余物理内存： %s G" % free)
    print("物理内存使用率： %s %%" % int(memory * 100))
    return free,total,memory

# 获取系统启动时间
def getStartTime():
    print("系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
    return datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

# 获取系统用户
def getCurrentUser():
    users_count = len(psutil.users())
    users_list = ",".join([u.name for u in psutil.users()])
    print("当前有%s个用户，分别是 %s" % (users_count, users_list))
    return users_count,users_list

def getWlan():
    io = psutil.disk_partitions()
    print('-----------------------------磁盘信息---------------------------------------')
    for i in io:
        try:
            o = psutil.disk_usage(i.device)
            print("总容量："+\
            str(int(o.total / (1024.0 * 1024.0 * 1024.0))) +\
            "G")
            print("已用容量："+\
            str(int(o.used / (1024.0 * 1024.0 * 1024.0)))+\
            "G")
            print("可用容量："+\
            str(int(o.free / (1024.0 * 1024.0 * 1024.0)))+\
            "G")
        except PermissionError:
            continue

def getpids():
    print('-----------------------------进程信息-------------------------------------')
    for pnum in psutil.pids():
        p = psutil.Process(pnum)
        print(
            "进程名 %-20s  内存利用率 %-18s 进程状态 %-10s 创建时间 %-10s " % (
            p.name(), p.memory_percent(), p.status(), p.create_time()))
    pass

if __name__ == '__main__':
    getCpuCount()
    getCpuPercent()
    getMem()
    getStartTime()
    getCurrentUser()
    getWlan()
    getpids()